package com.sky.wsp.drools.controller;

import cn.hutool.core.io.IoUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @desccription 在此输入描述
 * @auth wangshaopeng@talkweb.com.cn
 * @date 2022/6/27
 */
@RestController
@RequestMapping("file")
public class FileController {

    @GetMapping(value = "preview_pdf_1", produces = MediaType.APPLICATION_PDF_VALUE)
    public byte[] previewPdf1(HttpServletResponse response) throws FileNotFoundException {

        File file = new File("d:\\ali.pdf");
        boolean exists = file.exists();
        System.out.println(exists);

        FileInputStream fis = new FileInputStream(file);

        return IoUtil.readBytes(fis);
    }

    @GetMapping(value = "preview_pdf_2", produces = MediaType.APPLICATION_PDF_VALUE)
    public void previewPdf2(HttpServletResponse response) throws IOException {

        File file = new File("d:\\ali.pdf");
        boolean exists = file.exists();
        System.out.println(exists);

        FileInputStream fis = new FileInputStream(file);
        byte[] bytes = IoUtil.readBytes(fis);
        response.setContentType("application/pdf");
        IoUtil.write(response.getOutputStream(), true, bytes);

    }
}
